﻿#region

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraTab;
using DevTools.ClassStuffGenerator.Components;
using DevTools.ClassStuffGenerator.Controls;
using DevTools.Properties;

#endregion

namespace DevTools.ClassStuffGenerator.Forms
{
    public partial class ClassStuffGeneratorForm : RootForm
    {
        #region singleton

        private static ClassStuffGeneratorForm _instance;

        public static ClassStuffGeneratorForm Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new ClassStuffGeneratorForm();
                return _instance;
            }
        }

        private ClassStuffGeneratorForm()
        {
            InitializeComponent();

            for (int i = 1; i < 10; i++)
            {
                var tabPage = CreateTabPage(i);
                tbcTab.TabPages.Add(tabPage);
            }

            tbcTab.TabPages.Add(CreateTabPage(0));

            tbcTab.SelectedTabPageIndex = 0;

            grdClass.LoadDefaultSetting();
            grdField.LoadDefaultSetting();

            cheCopyToClipboard.Checked = Settings.Default.ClassStuffGenerator_CopyToClipboard;
        }

        #endregion

        public bool CopyToClipboardChecked
        {
            get { return cheCopyToClipboard.Checked; }
        }

        public string GetSelectedTemplate()
        {
            return tbcTab.SelectedTabPage.Controls[0].Text;
        }

        private XtraTabPage CreateTabPage(int templateIndex)
        {
            XtraTabPage tabPage = new XtraTabPage();
            tabPage.Text = string.Format("템플릿 (&{0})", templateIndex);

            TemplateControl templateControl = new TemplateControl(templateIndex);
            templateControl.Dock = DockStyle.Fill;

            tabPage.Tag = templateIndex;
            tabPage.Controls.Add(templateControl);

            return tabPage;
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            Settings.Default.ClassStuffGenerator_CopyToClipboard = cheCopyToClipboard.Checked;

            foreach (XtraTabPage tabPage in tbcTab.TabPages)
            {
                if (tabPage == tbpSource)
                    continue;

                TemplateControl templateControl = (TemplateControl) tabPage.Controls[0];
                templateControl.WriteToTemplate();
            }

            Settings.Default.Save();

            base.OnClosing(e);
        }

        public List<Item> GetItems()
        {
            var items = new List<Item>();

            int rowHandle = grvField.GetVisibleRowHandle(0);
            while (rowHandle != GridControl.InvalidRowHandle)
            {
                Item item = (Item)grvField.GetRow(rowHandle);
                items.Add(item);

                rowHandle = grvField.GetNextVisibleRow(rowHandle);
            }

            return items;
        }

        private void btnOpenAssembly_Click(object sender, EventArgs e)
        {
            if (ofdFile.ShowDialog() != DialogResult.OK)
                return;

            var items = Reflector.Load(ofdFile.FileName);
            bdsClass.DataSource = items;
        }

        private void tbcTab_SelectedPageChanged(object sender, TabPageChangedEventArgs e)
        {
            if (e.Page == tbpSource)
                return;

            TemplateControl templateControl = (TemplateControl)e.Page.Controls[0];
            templateControl.Generate();
        }
    }
}